home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / library / help / tcl / intro / syntax < prev   
Encoding:
Text File  |  1993-10-26  |  8.5 KB  |  166 lines  |  [TEXT/$Tcl]

  1.  
  2.  
  3.  
  4.      DESCRIPTION
  5.           The following rules define the syntax and semantics  of  the
  6.           Tcl language:
  7.  
  8.           [1]  A Tcl script is a string containing one  or  more  com-
  9.                mands.  Semi-colons and newlines are command separators
  10.                unless quoted as described below.  Close  brackets  are
  11.                command  terminators  during  command substitution (see
  12.                below) unless quoted.
  13.  
  14.           [2]  A command is evaluated in two steps.   First,  the  Tcl
  15.                interpreter  breaks the command into words and performs
  16.                substitutions as described below.  These  substitutions
  17.                are  performed  in  the same way for all commands.  The
  18.                first word is used to locate  a  command  procedure  to
  19.                carry  out  the  command,  then all of the words of the
  20.                command are passed to the command procedure.  The  com-
  21.                mand  procedure  is free to interpret each of its words
  22.                in any way it likes, such as an integer, variable name,
  23.                list,  or  Tcl  script.   Different  commands interpret
  24.                their words differently.
  25.  
  26.           [3]  Words of a command are separated by white space (except
  27.                for newlines, which are command separators).
  28.  
  29.           [4]  If the  first  character  of  a  word  is  double-quote
  30.                (``"'')  then  the  word  is  terminated  by  the  next
  31.                double-quote character.  If semi-colons,  close  brack-
  32.                ets,  or  white  space  characters (including newlines)
  33.                appear between the quotes  then  they  are  treated  as
  34.                ordinary  characters and included in the word.  Command
  35.                substitution, variable substitution, and backslash sub-
  36.                stitution  are  performed on the characters between the
  37.                quotes as described below.  The double-quotes  are  not
  38.                retained as part of the word.
  39.  
  40.           [5]  If the first character of  a  word  is  an  open  brace
  41.                (``{'')  then  the  word  is terminated by the matching
  42.                close brace (``}'').  Braces nest within the word:  for
  43.                each  additional open brace there must be an additional
  44.                close brace (however, if an open brace or  close  brace
  45.                within  the  word is quoted with a backslash then it is
  46.                not counted in locating the matching close brace).   No
  47.                substitutions  are  performed on the characters between
  48.                the braces except for  backslash-newline  substitutions
  49.                described  below,  nor  do semi-colons, newlines, close
  50.                brackets, or white space receive any special  interpre-
  51.                tation.   The  word will consist of exactly the charac-
  52.                ters between the outer braces, not including the braces
  53.                themselves.
  54.  
  55.           [6]  If a word contains an open  bracket  (``['')  then  Tcl
  56.                performs  command  substitution.  To do this it invokes
  57.                the Tcl interpreter recursively to process the  charac-
  58.                ters  following  the open bracket as a Tcl script.  The
  59.                script may contain any number of commands and  must  be
  60.                terminated  by  a close bracket (``]'').  The result of
  61.                the script (i.e. the result of  its  last  command)  is
  62.                substituted  into the word in place of the brackets and
  63.                all of the characters between them.  There may  be  any
  64.                number of command substitutions in a single word.  Com-
  65.                mand substitution is not performed on words enclosed in
  66.                braces.
  67.  
  68.           [7]  If a word contains a dollar-sign (``$'') then Tcl  per-
  69.                forms  variable  substitution:  the dollar-sign and the
  70.                following characters are replaced in the  word  by  the
  71.                value  of a variable.  Variable substition may take any
  72.                of the following forms:
  73.  
  74.                $name          Name is the name of a  scalar  variable;
  75.                               the  name is terminated by any character
  76.                               that isn't a letter,  digit,  or  under-
  77.                               score.
  78.  
  79.                $name(index)   Name gives the name of an array variable
  80.                               and  index  gives the name of an element
  81.                               within that array.   Name  must  contain
  82.                               only  letters,  digits, and underscores.
  83.                               Command substitutions, variable  substi-
  84.                               tutions, and backslash substitutions are
  85.                               performed on the characters of index.
  86.  
  87.                ${name}        Name is the name of a  scalar  variable.
  88.                               It may contain any characters whatsoever
  89.                               except for close braces.
  90.  
  91.           There may be any number of variable substitutions in a  sin-
  92.           gle  word.   Variable substitution is not performed on words
  93.           enclosed in braces.
  94.  
  95.           [8]  If a backslash  (``\'')  appears  within  a  word  then
  96.                backslash  substitution occurs.  In all cases but those
  97.                described below the backslash is dropped and  the  fol-
  98.                lowing  character  is  treated as an ordinary character
  99.                and included in the word.  This allows characters  such
  100.                as  double  quotes, close brackets, and dollar signs to
  101.                be included in words without  triggering  special  pro-
  102.                cessing.   The  following  table  lists  the  backslash
  103.                sequences that are handled specially,  along  with  the
  104.                value that replaces each sequence.
  105.  
  106.                \a    Audible alert (bell) (0x7).
  107.  
  108.                \b    Backspace (0x8).
  109.  
  110.                \f    Form feed (0xc).
  111.  
  112.                \n    Newline (0xa).
  113.  
  114.                \r    Carriage-return (0xd).
  115.  
  116.                \t    Tab (0x9).
  117.  
  118.                \v    Vertical tab (0xb).
  119.  
  120.                \<newline>whiteSpace
  121.                      A single space character replaces the  backslash,
  122.                      newline,  and  all white space after the newline.
  123.                      This backslash sequence is unique in that  it  is
  124.                      replaced  in  a separate pre-pass before the com-
  125.                      mand is actually parsed.  This means that it will
  126.                      be  replaced  even when it occurs between braces,
  127.                      and the resulting space will be treated as a word
  128.                      separator if it isn't in braces or quotes.
  129.  
  130.                \\    Backslash (``\'').
  131.  
  132.                \ooo  The digits ooo (one, two, or three of them)  give
  133.                      the octal value of the character.
  134.  
  135.                \xhh  The hexadecimal digits hh  give  the  hexadecimal
  136.                      value of the character.  Any number of digits may
  137.                      be present.
  138.  
  139.           Backslash substitution is not performed on words enclosed in
  140.           braces, except for backslash-newline as described above.
  141.  
  142.           [9]  If a hash character (``#'') appears at  a  point  where
  143.                Tcl  is expecting the first character of the first word
  144.                of a command, then the hash character and  the  charac-
  145.                ters  that  follow it, up through the next newline, are
  146.                treated as a comment and ignored.  The comment  charac-
  147.                ter only has significance when it appears at the begin-
  148.                ning of a command.
  149.  
  150.           [10] Each character is processed exactly  once  by  the  Tcl
  151.                interpreter as part of creating the words of a command.
  152.                For example, if  variable  substition  occurs  then  no
  153.                further  substitions  are performed on the value of the
  154.                variable;  the value is inserted into the  word  verba-
  155.                tim.   If  command  substitution occurs then the nested
  156.                command is processed entirely by the recursive call  to
  157.                the  Tcl  interpreter;  no  substitutions  are perfomed
  158.                before making the recursive call and no additional sub-
  159.                stitutions  are  performed  on the result of the nested
  160.                script.
  161.  
  162.           [11] Substitutions do not affect the word  boundaries  of  a
  163.                command.  For example, during variable substitution the
  164.                entire value of the variable becomes part of  a  single
  165.                word, even if the variable's value contains spaces.
  166.